import React, { useEffect, useState } from 'react';

import { stock_list } from '../../utils/api/zqq/index'
import store from '../../store/index'
// 组件引入
import StockListTop from './stocklist/StockListTop'
import StockListTable from './stocklist/StockListtable'
import { stock_list_create, stock_list_delete, stock_list_update } from '../../utils/api/zqq/index'
interface Props { }
function StockList(props: Props) {
    const [stocklistdata, setStocklistdata]: any = useState([])
    const stockList = () => {
        stock_list().then((res) => {
            console.log(res.data);
            setStocklistdata(res.data);
            store.dispatch({ type: 'LIST', payload: res.data })
        })
    }
    //创建新的stock
    const stocklistcreat = (data: Object) => {
        stock_list_create(data).then((res: any) => {
            console.log(res.data);
            store.dispatch({ type: 'ADD', payload: res.data })
            stockList()
        })
    }

    useEffect(() => {
        stockList()
    }, [])
    //search
    const changestockdata = (value: []) => {
        setStocklistdata(value)
    }
    // delete
    const stockdelete = (value: any) => {
        // console.log(value);
        stock_list_delete(value).then((res) => {
            console.log(res);
            store.dispatch({ type: 'DELETE', payload: res.data })
            stockList()
        })
    }
    //update
    const stockupdate = (value: any) => {
        console.log(value);
        stock_list_update(value).then((res) => {
            console.log(res);
            store.dispatch({ type: 'DELETE', payload: res.data })
            stockList()
        })
    }
    return (
        <div>
            <StockListTop stocklistcreat={stocklistcreat} changestockdata={changestockdata} />
            <StockListTable stockupdate={stockupdate} stockdelete={stockdelete} stocklistdata={stocklistdata} />
        </div>
    );
}

export default StockList;